import json
import requests
# WebHDFS的基本URL和HDFS路径
webhdfs_url = 'http://192.168.135.6:9870/webhdfs/v1'
hdfs_path = '/tempoutput/part-r-00000'

hadoop_username = 'hdfs'

full_url = f"{webhdfs_url}{hdfs_path}?op=OPEN&user.name={hadoop_username}"

# 本地保存文件的路径
local_file_path = 'part-r-00000_local'

# 发送HTTP GET请求以打开文件
response = requests.get(full_url, stream=True)

# 检查响应状态码
if response.status_code == 200:
    # 如果成功，将响应内容写入本地文件
    with open(local_file_path, 'wb') as f:
        for chunk in response.iter_content(chunk_size=8192):
            if chunk:  # 确保chunk不为空
                f.write(chunk)
    print(f"File downloaded successfully to {local_file_path}")
else:
    print(f"Error: {response.status_code} - {response.text}")
with open('part-r-00000_local', 'r') as file:
    lines = file.readlines()


data = []


for line in lines:

    date, temperature = line.strip().split('\t')

    data_point = {'date': date, 'temperature': float(temperature)}
    # 将数据点添加到列表中
    data.append(data_point)

# 将数据列表转换为JSON字符串
json_data = json.dumps(data, indent=2)  # indent=2 用于美化输出，使JSON更易于阅读

# 将JSON字符串写入到data.js文件中
with open('data.js', 'w') as file:
    file.write('var chartData = ' + json_data + ';')
